package org.hgvc.webapp.ets.jsf.beans.admin;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import org.hgvc.webapp.ets.bo.PartnerDocument;
import org.hgvc.webapp.ets.constant.PartnerConstant;
import org.hgvc.webapp.ets.spring.service.PartnerService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.SpringUtils;


public class AdminPartnerDocumentMasterBean extends AbstractBean{

	public static final String BEAN_NAME="adminPartnerDocumentMasterBean";
	private PartnerDocument document;
	private PartnerService service;
	private List<SelectItem> typeList;
	private List<PartnerDocument> docList;
	public static final String NAV_EDIT_ACTION="adminEditPartnerDoc";
	public static final String NAV_CREATE_ACTION="adminCreatePartnerDoc";
	public static final String NAV_LIST_ACTION="adminPartnerDocList";

	public PartnerDocument getDocument() {
		return document;
	}
	public void setDocument(PartnerDocument document) {
		this.document = document;
	}
	public void setService(PartnerService service) {
		this.service = service;
	}

	/*------------------------------------------------------------------*/
	public List<SelectItem> getTypeList() {

		typeList = new ArrayList<SelectItem>();
		SelectItem item1 = new SelectItem(PartnerConstant.PARTNER_DOC_MANDATORY,
				SpringUtils.getMessage(PartnerConstant.PARTNER_DOC_MANDATORY, new Object[]{}));
		SelectItem item2 = new SelectItem(PartnerConstant.PARTNER_DOC_NOT_MANDATORY,
				SpringUtils.getMessage(PartnerConstant.PARTNER_DOC_NOT_MANDATORY, new Object[]{}));
		typeList.add(item1);
		typeList.add(item2);
		return typeList;
	}
	public List<PartnerDocument> getDocList() {
		try{
			docList = service.getPartnerDocumentList();
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return docList;
	}

	public String navCreateAction(){
		document = new PartnerDocument();
		return NAV_CREATE_ACTION;
	}
	
	public String createAction(){
		try{
			service.createPatnerDocument(document, getThisUser());
			return NAV_LIST_ACTION;
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		
		return NAV_CREATE_ACTION;
	}

	public void editActionListener(ActionEvent ae){
		UIParameter param = (UIParameter)ae.getComponent().findComponent("docId");
		String id = (String)param.getValue();

		for(PartnerDocument doc:docList){
			if(doc.getPartnerDocId().equalsIgnoreCase(id)){
				document = doc;
				break;
			}
		}
	}

	public String navEditAction(){
		return NAV_EDIT_ACTION;
	}

	public String editAction(){
		try{
			service.updatePatnerDocument(document, getThisUser());
			return NAV_LIST_ACTION;
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		
		return NAV_EDIT_ACTION;
	}

	public String deleteAction(){
		try{
			service.deletePatnerDocument(docList, getThisUser());
			return NAV_LIST_ACTION;
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return NAV_LIST_ACTION;
	}
	
	public String navListAction(){
		return NAV_LIST_ACTION;
	}
}
